package com.pat.web.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.pat.common.Constants;
import com.pat.web.ApiResponse;
import com.pat.web.model.Pet;
import com.pat.web.services.PetService;

public class PetController extends Controller {

	public void checkNickname() {
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		boolean existed = PetService.checkNickname(userId, getPara("nickname"));
		ApiResponse response = new ApiResponse();
		if (existed) {
			response.setData("宠物昵称已存在");
			response.setStatusCode("2003");
			renderJson(response);
			return;
		}
		response.setData("宠物昵称不存在");
		response.setStatusCode("0000");
		renderJson(response);
	}

	// 创建宠物
	public void createPet() {
		Pet pet = getBean(Pet.class, "");
		ApiResponse response = new ApiResponse();
		// 判断同一用户宠物名是否重复
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		boolean existed = PetService.checkNickname(userId, pet.getPetNick());
		if (existed) {
			response.setData("宠物昵称已存在");
			response.setStatusCode("2003");
			renderJson(response);
			return;
		}
		pet.setUserId(userId).setCreateUserId(userId).setUpdateUserId(userId).setCreateTime(new Date())
				.setUpdateTime(new Date()).save();
		response.setData(pet.get("id"));
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void getSelfPet() {
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		List<Pet> pets = Pet.dao.find(
				"select id,pet_photo petPhoto,pet_nick petNick from pet where user_id = ? and is_deleted = 0", userId);
		ApiResponse response = new ApiResponse();
		response.setData(pets);
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void getPetById() {
		long petId = getParaToLong("petId");
		Record pet = Db.findFirst(
				"select p.id,p.user_id userId,p.pet_photo petPhoto,p.pet_nick petNick,p.pet_category_id petCategoryId,p.pet_sex petSex,"
						+ "p.is_sterilized isSterilized,p.pet_birthday petBirthday,p.weight,p.blood_type bloodType,p.create_time createTime,p.update_time updateTime,"
						+ "pc.category_name categoryName from pet p join pet_category pc on p.pet_category_id = pc.id where p.id = ? and p.is_deleted = 0",
				petId);
		ApiResponse response = new ApiResponse();
		response.setData(pet);
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void updatePet() {
		Pet pet = getBean(Pet.class, "");
		ApiResponse response = new ApiResponse();
		if (pet == null || pet.getId() == null) {
			response.setStatusCode("2004");
			renderJson(response);
			return;
		}
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		pet.setUpdateTime(new Date()).setUpdateUserId(userId).update();
		response.setData("success");
		response.setStatusCode("0000");
		renderJson(response);
	}

	public void delPet() {
		long petId = getParaToLong("petId");
		Long userId = getAttr(Constants.LOGIN_USER_ID);
		int result = Db.update("update pet set is_deleted = 1 where user_id = ? and id = ?", userId, petId);
		ApiResponse response = new ApiResponse();
		response.setData(result);
		response.setStatusCode("0000");
		renderJson(response);
	}
}
